package com.yuntongxun.ecdemo.voicechat.ui.activity;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.EditText;

import com.loopj.android.http.RequestParams;
import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.common.dialog.ECProgressDialog;
import com.yuntongxun.ecdemo.voicechat.api.ApiConstants;
import com.yuntongxun.ecdemo.voicechat.comman.Contants;
import com.yuntongxun.ecdemo.voicechat.presenter.ICommonRequestPresenter;
import com.yuntongxun.ecdemo.voicechat.presenter.impl.CommonRequestPresenterImpl;
import com.yuntongxun.ecdemo.voicechat.util.PreferenceUtils;
import com.yuntongxun.ecdemo.voicechat.util.iview.ICommonViewUi;

import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/12/27.
 */

public class ReportActivity extends BaseChatActivity implements ICommonViewUi {

    @InjectView(R.id.reportEdt)
    EditText reportEdt;
    @InjectView(R.id.submit_btn)
    Button submitBtn;
    private ICommonRequestPresenter iCommonRequestPresenter;

    private String reportAccount="";

    public static final String REPORTACCOUNT="reportaccount";

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_report;
    }

    @Override
    protected void initViewsAndEvents() {
        setTitle("Report");
        iCommonRequestPresenter = new CommonRequestPresenterImpl(mContext, this);
        reportAccount=getIntent().getStringExtra(REPORTACCOUNT);

        if ( !PreferenceUtils.getPrefString(this, Contants.Preference.REPORT,"").equals("")){
            reportEdt.setText(PreferenceUtils.getPrefString(this, Contants.Preference.REPORT,""));
        }
    }

    @OnClick(R.id.submit_btn)
    public void onSubmitBtnClick(){
        if (TextUtils.isEmpty(reportEdt.getText().toString().trim())){
            showToastLong("Enter your reason for reporting");
            return;
        }
        toRequest(ApiConstants.EventTags.BEGIN_EVENT);

    }

    @Override
    public void toRequest(int eventTag) {
        RequestParams map = new RequestParams();
        map.put("reportAccount", reportAccount);
        map.put("feedBack", reportEdt.getText().toString().trim());
        iCommonRequestPresenter.request(eventTag, mContext, ApiConstants.Urls.FEEDBACK_ADD, map);
    }

    @Override
    public void getRequestData(int eventTag, String result) {
//        showToastLong(HttpStatusUtil.getStatusMsg(result));
        reportEdt.setText("");
        finish();
    }

    @Override
    public void onRequestSuccessException(int eventTag, String msg) {
        showToastLong(msg);
    }

    @Override
    public void onRequestFailureException(int eventTag, String msg) {
        showToastLong(msg);
    }

    private ECProgressDialog mPostingdialog;
    @Override
    public void isRequesting(int eventTag, boolean status) {
        if (status) {
            mPostingdialog = new ECProgressDialog(this, "Waiting");
            mPostingdialog.show();
        } else {
            mPostingdialog.dismiss();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            if (!TextUtils.isEmpty(reportEdt.getText().toString().trim())){
                PreferenceUtils.setPrefString(this, Contants.Preference.REPORT,reportEdt.getText().toString());
            }
        }
        return super.onKeyDown(keyCode, event);
    }

}
